<?php

/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');

class BH_RSS_Widget extends BH_Widget_Base {

	function widget($args) {
		require_once(ABSPATH . WPINC . '/rss-functions.php');
		extract($args);

		$style = $this->get_attribute('style');
	
		$num_items = (int) $this->get_attribute('items');
		$show_summary = $this->get_attribute('show_summary');
		$rss = $this->get_attribute('rss');
		$rss_time = $this->get_attribute('rss-time');
		$now = time();
	
		if ( empty($num_items) || $num_items < 1 || $num_items > 10 )
			$num_items = 10;
	
		$url = $this->get_attribute('url');
	
		if ( empty($url) ) 
			return;
	
		while ( strstr($url, 'http') != $url )
			$url = substr($url, 1);
		
		$expire_time = $this->get_attribute('expire-time');
		$expire_time = $expire_time ? $expire_time : 1800;
			
			
		if ((!$rss) || (($now - $rss_time) > $expire_time))  {
			$rss = fetch_rss($url);
			$rss_time = $now;
			$this->set_attribute('rss',$rss);
			$this->set_attribute('rss-time', $rss_time);
		}
	
		$link = wp_specialchars(strip_tags($rss->channel['link']), 1);
	
		while ( strstr($link, 'http') != $link )
			$link = substr($link, 1);
		
		$desc = wp_specialchars(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)), 1);

		$title = $this->get_attribute('title');

		if ( empty($title) )
			$title = htmlentities(strip_tags($rss->channel['title']));

		if ( empty($title) )
			$title = $desc;

		if ( empty($title) )
			$title = "Unknown Feed";

		$url = wp_specialchars(strip_tags($url), 1);

		if ( file_exists(dirname(__FILE__) . '/rss.png') )
			$icon = str_replace(ABSPATH, get_settings('siteurl').'/', dirname(__FILE__)) . '/rss.png';
		else
			$icon = get_settings('siteurl').'/wp-includes/images/rss.png';

		$title = "<a class='rsswidget' href='$url' title='Syndicate this content'><img width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);
	
		if ($title)  print $before_title . $title . $after_title ."\n";
		
		if ( $rss ) {
		    echo "$before_list\n";
			$rss->items = array_slice($rss->items, 0, $num_items);

			foreach ($rss->items as $item ) {
				while ( strstr($item['link'], 'http') != $item['link'] )
					$item['link'] = substr($item['link'], 1);

				$link = wp_specialchars(strip_tags($item['link']), 1);
				$title = wp_specialchars(strip_tags($item['title']), 1);

				if ( empty($title) )
					$title = __('Untitled');

				if ( $show_summary ) {
					$desc = '';
					$summary = '<div class="rssSummary">' . $item['description'] . '</div>';
				} else {
					$desc = str_replace(array("\n", "\r"), ' ', wp_specialchars(strip_tags(html_entity_decode($item['description'], ENT_QUOTES)), 1));
					$summary = '';
				}

				echo $before_item ."<a class='rsswidget' href='$link' title='$desc'>$title</a>". $summary . $after_item . "\n";			
			}
			
			echo "$after_list\n";
		} else {
			echo $before_item. "An error has occured; the feed is probably down. Try again later" . $after_item . "\n";
		}
		
		echo $this->get_widget_end_tag($args);

		echo "$after_widget\n";
	}

	function control() {
		$dashed_name = preg_replace("/\s+/", "-", $this->get_name());
		
		$style = $this->get_attribute('style');
		$title = (int) $this->get_attribute('title');
		$items = (int) $this->get_attribute('items');
		$url = $this->get_attribute('url');

		if ( $_POST["bh-rss-". $dashed_name ."-submit"] ) {
			$num_items = (int) $_POST["bh-rss-items-$dashed_name"];
			$url = strip_tags(stripslashes($_POST["bh-rss-url-$dashed_name"]));
			$title = trim(strip_tags(stripslashes($_POST["bh-rss-title-$dashed_name"])));
			$style = trim(strip_tags(stripslashes($_POST["bh-rss-style-$dashed_name"])));
			
			$this->set_attribute('style',$style);
			$this->set_attribute('title',$title);
			$this->set_attribute('items',$items);
			$this->set_attribute('url', $url);
		}

		if ( empty($items) || $items < 1 )
			$items = 10;
?>
			
			<label>Style</label><input id="rss-style-<?php echo "$dashed_name"; ?>" name="bh-rss-style-<?php echo "$dashed_name"; ?>" type="text" value="<?php echo $style; ?>" /><br/>
			<label>Enter the RSS feed URL here:</label><br/>
			<input style="width: 400px;" id="rss-url-<?php echo "$dashed_name"; ?>" name="bh-rss-url-<?php echo "$dashed_name"; ?>" type="text" value="<?php echo $url; ?>" /><br/>
			<label>Title(optional):</label><input id="rss-title-<?php echo "$dashed_name"; ?>" name="bh-rss-title-<?php echo "$dashed_name"; ?>" type="text" value="<?php echo $title; ?>" /><br/>
			<label><?php _e('Number of items to display:'); ?></label>
			<select id="rss-items-<?php echo $n; ?>" name="bh-rss-items-<?php echo $dashed_name; ?>">
			  <?php for ( $i = 1; $i <= 10; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?>
		 	</select>
			<input type="hidden" id="bh-rss-<?php echo "$dashed_name"; ?>-submit" name="bh-rss-<?php echo "$dashed_name"; ?>-submit" value="1" />
<?php
	}


	function head() {

		echo "<style type=\"text/css\">a.rsswidget{display:inline !important;}a.rsswidget img{background:orange;color:white;}</style>\n";
	}

}


bh_widgets_register_class('BH_RSS_Widget');
?>
